package gold.digger;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Stack;

/**
 * Created by fanzhenyu02 on 2020/2/27.
 */
public class LC496 {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Map<Integer, Integer> dict = getSingleNextGreaterElement(nums2);
        int[] res = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            res[i] = dict.get(nums1[i]);
        }

        return res;
    }


    public Map<Integer, Integer> getSingleNextGreaterElement(int[] arr) {
        Map<Integer, Integer> dict = new LinkedHashMap<>();
        Stack<Integer> st = new Stack<>();
        for (int i = arr.length - 1; i >= 0; i--) {
            while (!st.isEmpty() && st.peek() <= arr[i]) {
                st.pop();
            }

            dict.put(arr[i], st.isEmpty() ? -1 : st.peek());
            st.push(arr[i]);
        }

        return dict;
    }

    /**
     * Input: s = "PAYPALISHIRING", numRows = 3
     * Output: "PAHNAPLSIIGYIR"
     */
    public static void main(String[] args) {
        LC496 an = new LC496();

        //Input: nums1 = [4,1,2],
        int[] nums2 = {1, 3, 4, 2};
        System.out.println(an.getSingleNextGreaterElement(nums2));


    }
}
